package startupcompanymanager2012;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Pathfinder extends JPanel {

    private final int WIDTH = 500, HEIGHT = 500;
    private final int DELAY = 25, IMAGE_SIZE = 35;
    private ImageIcon image, background, pc, chair;
    public static boolean pause = true;
    public Timer timer;
    private int i, j, x, y, x1, y1, move = 5, counter = 0,
            distances[] = {130, 360, 80, 0, 80, 360, 130}
            //,distances2[] = {130, 300, 0, 300, 130}
            ;
    private String direction,
            directions[] = {"SOUTH", "WEST", "SOUTH", "MIDDLE", "NORTH", "EAST", "NORTH"}
            //,directions2[] = {"SOUTH", "WEST", "MIDDLE", "EAST", "NORTH"}
            ;

    public Pathfinder() {
        timer = new Timer(DELAY, new PathListener());
        image = new ImageIcon("happyFace.gif");        
        background = new ImageIcon("garage.png");
        pc = new ImageIcon("pc.png");
        chair = new ImageIcon("stuhl.png");
        x1 = x = 500 - IMAGE_SIZE;
        y1= y = -50;
        direction = directions[0];
        i = 0;
        j = 0;

        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(new Color(204, 204, 255));

        timer.start();
    }

    public void paintComponent(Graphics page) {
        super.paintComponent(page);
        background.paintIcon(this, page, 0, 0);
        if (Core.devs.size()>1) {            
            pc.paintIcon(this, page, 130, 10);
            chair.paintIcon(this, page, 130, 80);
            image.paintIcon(this, page, 145, 90);            
        }        
        image.paintIcon(this, page, x, y);                        
        //System.out.println(x + "/" + y);        
    }

    public class PathListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (!pause) {
                j++;
                if (direction.equals("NORTH")) {
                    y -= move;
                } else if (direction.equals("SOUTH")) {
                    y += move;
                } else if (direction.equals("EAST")) {
                    x += move;
                } else if (direction.equals("WEST")) {
                    x -= move;
                }

                if (j * move > distances[i]) {
                    i++;
                    j = 0;
                    if (i >= distances.length) {
                        i = 0;
                    }
                    direction = directions[i];
                }
            }

            if (i == distances.length / 2) {
                pause = true;
                counter += 1;
            }

            if (counter >= 500) {
                pause = false;
                counter = 0;
            }                   
            
            repaint();
        }
    }
}